package huxibianjie.com.lbscandroid.ui.new_activity;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import huxibianjie.com.lbscandroid.R;
import huxibianjie.com.lbscandroid.base.BaseActivity;
import huxibianjie.com.lbscandroid.manager.UserManager;
import huxibianjie.com.lbscandroid.model.UserModel;
import huxibianjie.com.lbscandroid.task.Task;
import huxibianjie.com.lbscandroid.ui.Service;
import huxibianjie.com.lbscandroid.util.StringUtils;
import huxibianjie.com.lbscandroid.widget.CountdownTextView;

/**
 * 注册 页面
 */
public class RegisterActivity extends BaseActivity {

    @BindView(R.id.et_phone_number)
    EditText mPhoneNumber;
    @BindView(R.id.del_phone_number)
    ImageView mDelPhoneNumber;
    @BindView(R.id.et_phone_code)
    EditText mPhoneCode;
    @BindView(R.id.get_code)
    CountdownTextView countdownTextView;
    @BindView(R.id.inv_code)
    EditText inCode;
    @BindView(R.id.inv_invite_code)
    EditText invInviteCode;


    @Override
    protected int getContentView() {
        return R.layout.activity_register;
    }


    protected void initView() {
        initTitle(R.drawable.ic_back, "注册");
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        mPhoneNumber.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    mDelPhoneNumber.setVisibility(View.VISIBLE);
                } else {
                    mDelPhoneNumber.setVisibility(View.INVISIBLE);
                }
            }
        });
    }

    @OnClick({R.id.del_phone_number, R.id.get_code, R.id.btn_register})
    public void onClick(View v) {
        String phone = mPhoneNumber.getText().toString();
        switch (v.getId()) {
            case R.id.del_phone_number:
                mPhoneNumber.setText("");
                break;
            case R.id.get_code:
                if (!StringUtils.isMobile(phone)) {
                    showToast("请输入正确的手机号");
                    return;
                }
                showLoading();
                Task.create(Service.class).sentCode(phone).enqueue(new huxibianjie.com.lbscandroid.task.Callback<Map<String, String>>() {
                    @Override
                    public void onSuccess(@Nullable Map<String, String> map) {
                        cancelLoading();
                        showToast("验证码已发送,请注意查收");
                        countdownTextView.start();
                    }

                    @Override
                    public void onFailure(@NonNull String error) {
                        cancelLoading();
                        showToast(error);
                    }
                });
                break;
            case R.id.btn_register:
                String code = mPhoneCode.getText().toString().trim();
                String pass = inCode.getText().toString().trim();
                String inviteCode = invInviteCode.getText().toString();
                if (!StringUtils.isMobile(phone)) {
                    showToast("请输入正确的手机号");
                    return;
                }
                if (!isLetterDigit(pass)) {
                    showToast("密码格式不正确，请重新输入，字母+数字。");
                    inCode.setText("");
                    return;
                }
                if (code.length() == 0) {
                    showToast("请输入验证码");
                    return;
                }
                if (!inviteCode.equals("") && inviteCode.length() != 6) {
                    showToast("邀请码格式错误");
                    return;
                }
                if (inviteCode.equals("")) {
                    showLoading();
                    Task.create(Service.class).register(phone, code, pass).enqueue(new huxibianjie.com.lbscandroid.task.Callback<UserModel>() {
                        @Override
                        public void onSuccess(@Nullable UserModel userModel) {
                            cancelLoading();
                            if (userModel != null) {
                                UserManager.getInstance().save(userModel);
                                showToast("注册成功");
                                finish();
                            } else {
                                showToast("注册失败");
                            }
                        }

                        @Override
                        public void onFailure(@NonNull String error) {
                            cancelLoading();
                            showToast(error);
                        }
                    });
                } else {
                    showLoading();
                    Task.create(Service.class).register(phone, code, pass, inviteCode).enqueue(new huxibianjie.com.lbscandroid.task.Callback<UserModel>() {
                        @Override
                        public void onSuccess(@Nullable UserModel userModel) {
                            cancelLoading();
                            if (userModel != null) {
                                UserManager.getInstance().save(userModel);
                                showToast("注册成功");
                                finish();
                            } else {
                                showToast("注册失败");
                            }
                        }

                        @Override
                        public void onFailure(@NonNull String error) {
                            cancelLoading();
                            showToast(error);
                        }
                    });
                }

                break;
        }
    }

    /**
     * 规则：至少包含大小写字母及数字中的两种
     */
    private boolean isLetterDigit(String str) {
        return str.matches("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$");
    }
}


